package com.infinitus.nginxInfo.service.impl;

import java.io.Serializable;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.infinitus.nginxInfo.domain.FilterCountLog;
import com.infinitus.nginxInfo.domain.FilterLog;
import com.infinitus.nginxInfo.service.KafkaLogService;
import com.infinitus.nginxInfo.service.util.TimeUtil;


@Service
public class KafkaLogServiceImpl implements KafkaLogService {

	@Autowired
	private EntityManagerFactory entityManagerFactory;
	
	@Override
	@Transactional
	public void saveCountLog(FilterCountLog countLog) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		try{
			Date the_date = countLog.getTheDate();
			if(the_date==null){
				the_date = new Date();
			}
			
			try {
				the_date = TimeUtil.dateFormat.parse(TimeUtil.dateFormat.format(the_date));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			List<FilterCountLog> resultList = entityManager.createNamedQuery("FilterCountLog.findByDateAndTopic",FilterCountLog.class ).setParameter("theDate", the_date).setParameter("receiverTopic", countLog.getReceiverTopic()).getResultList();
			
			if(resultList.size() == 0) {
				entityManager.merge(countLog);
			} else {
				countLog.setId(resultList.get(0).getId());
				entityManager.merge(countLog);
			}
			entityManager.flush();
			transaction.commit();
		}catch(Exception e){
			transaction.rollback();
			e.printStackTrace();
		}finally {
			entityManager.clear();
			entityManager.close();
		}
	}
	
	@Override
	public void saveFilterLog(FilterLog filterLog) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		try{
			List<FilterLog> resultList = entityManager.createNamedQuery("FilterLog.findUnique",FilterLog.class )
					.setParameter("ReceiverTopic", filterLog.getReceiverTopic()).setParameter("OsPlat", filterLog.getOsPlat())
					.setParameter("ReceiverDate", filterLog.getReceiverDate()).setParameter("ReceiverHour", filterLog.getReceiverHour())
					.setParameter("EventDate", filterLog.getEventDate()).setParameter("SystemName", filterLog.getSystemName())
					.setParameter("FilterType", filterLog.getFilterType()).getResultList();
			if(resultList.size() == 0) {
				entityManager.merge(filterLog);
			} else {
				filterLog.setId(resultList.get(0).getId());
				entityManager.merge(filterLog);
			}
			entityManager.flush();
			transaction.commit();
		}catch(Exception e){
			transaction.rollback();
			e.printStackTrace();
		}finally {
			entityManager.clear();
			entityManager.close();
		}
		
	}

	@Override
	public void save(Serializable serializable) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		try{
			entityManager.merge(serializable);
			entityManager.flush();
			transaction.commit();
		}catch(Exception e){
			transaction.rollback();
			e.printStackTrace();
		}finally {
			entityManager.clear();
			entityManager.close();
		}
	}

	private void setParams(Query nativeQuery, Map<String, Object> paramMap) {
		for(Entry<String, Object> entry:  paramMap.entrySet()){
			nativeQuery.setParameter(entry.getKey(), entry.getValue());
		}
	}

}
